package com.help.knowhelps.ui;

import java.io.File;

import com.help.knowhelps.R;
import com.help.knowhelps.commond.Utils;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.ContentResolver;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Bundle;
import android.os.SystemClock;
import android.provider.MediaStore;
import android.view.View;
import android.view.Window;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.Toast;
import android.widget.LinearLayout.LayoutParams;

public class AskingHelpActivity extends Activity{

	EditText et_entity;
	LinearLayout location,ll_pictures;
	View iv_camera,iv_submit,iv_back;
	File sdcardTempFile;
	private LayoutParams params;
	private int crop = Utils.dip2px(this, 44); 
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.askinghelp_activity);
		initComponent();
	}
	private void initComponent()
	{
		//当前页面actionbar与其他界面不同,因此不调用初始化actionbar的方法
		params = new LayoutParams(Utils.dip2px(this, 44), Utils.dip2px(this, 44));
    	params.setMargins(Utils.dip2px(this, 8), 0, Utils.dip2px(this, 8), 0);
		et_entity = (EditText) findViewById(R.id.asking_editText);
		location = (LinearLayout) findViewById(R.id.asking_layout_location);
		iv_camera = findViewById(R.id.asking_actionbar_imageView_camera);
		iv_back = findViewById(R.id.asking_actionbar_imageView_back);
		iv_submit = findViewById(R.id.asking_imageView_submit);
		ll_pictures = (LinearLayout) findViewById(R.id.asking_pictures_layout);
		location.setOnClickListener(clicklistener);
		iv_camera.setOnClickListener(clicklistener);
		iv_back.setOnClickListener(clicklistener);
		iv_submit.setOnClickListener(clicklistener);
		sdcardTempFile = new File("/mnt/sdcard/", "tmp_pic_" + SystemClock.currentThreadTimeMillis() + ".jpg");
	}
	private OnClickListener clicklistener = new OnClickListener() {
		public void onClick(View v) {
			switch (v.getId()) {
			case R.id.asking_actionbar_imageView_back:
				finish();
				break;
			case R.id.asking_actionbar_imageView_camera:
				AlertDialog dialog = null;
					if (dialog == null) {                 
						dialog = new AlertDialog.Builder(AskingHelpActivity.this)
						.setItems(new String[] { "相机", "相册" }, new DialogInterface.OnClickListener() {                     
							@Override                    
							public void onClick(DialogInterface dialog, int which) {                        
								if (which == 0) {                             
									Intent intent = new Intent("android.media.action.IMAGE_CAPTURE");          
									intent.putExtra("output", Uri.fromFile(sdcardTempFile));                             
									intent.putExtra("crop", "true");                             
									intent.putExtra("aspectX", 1);// 裁剪框比例                             
									intent.putExtra("aspectY", 1);                             
									intent.putExtra("outputX", crop);// 输出图片大小                             
									intent.putExtra("outputY", crop);                             
									startActivityForResult(intent, 101);                         
								} else {                             
									Intent intent = new Intent("android.intent.action.PICK");                             
									intent.setDataAndType(MediaStore.Images.Media.INTERNAL_CONTENT_URI, "image/*");                             
									intent.putExtra("output", Uri.fromFile(sdcardTempFile));                             
									intent.putExtra("crop", "true");                             
									intent.putExtra("aspectX", 1);// 裁剪框比例                             
									intent.putExtra("aspectY", 1);                             
									intent.putExtra("outputX", crop);// 输出图片大小                             
									intent.putExtra("outputY", crop);                             
									startActivityForResult(intent, 100);                         
									}                     
								}                 
							}).create();             
						}             
					if (!dialog.isShowing()) {                 
						dialog.show();             
						}         
				break;
			case R.id.asking_layout_location:
//				startActivityForResult(new Intent(AskingHelpActivity.this,Map.....), 102);  
				break;
			case R.id.asking_imageView_submit:
				String entity = et_entity.getText().toString();
				if(null == entity||entity.equals(""))
				{
					Toast.makeText(AskingHelpActivity.this, "请求不可以不填哦...!", Toast.LENGTH_SHORT).show();
				}
				// do...   上传帮助信息
				finish();
				break;
			default:
				break;
			}
		}
	};

	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);
		if (resultCode == RESULT_OK) {
			//
			ContentResolver resolver = getContentResolver();
			// 照片的原始资源地址
			Uri originalUri = data.getData();
			try {
//				 使用ContentProvider通过URI获取原始图片
				Bitmap photo = MediaStore.Images.Media.getBitmap(resolver,originalUri);
				if (photo != null) {
//					 为防止原始图片过大导致内存溢出，这里先缩小原图显示，然后释放原始Bitmap占用的内存
//					 Bitmap smallBitmap = ... ;// 释放原始图片占用的内存，防止out of memory异常发生
//					 photo.recycle();
//					 ll_pictures.setVisibility(View.VISIBLE);
//					 ImageView iv_temp = new ImageView(AskingHelpActivity.this);
////					 iv_temp.setImageBitmap(Utils.convertToBitmap(picture,Utils.dip2px(AskingHelpActivity.this,44) ,Utils.dip2px(AskingHelpActivity.this, 44)));
//					 ll_pictures.addView(iv_temp, params);
					 }
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	}
}
